জাভা জেনেরিক্স মূলত Compile-Time টাইপ সেফটি নিশ্চিত করে। জেনেরিক্স ব্যবহার করলে কোড রানটাইমে টাইপ-রিলেটেড ত্রুটি থেকে মুক্ত থাকে এবং কোড আরও মডুলার এবং পুনঃব্যবহারযোগ্য হয়। এছাড়াও, জেনেরিক্সের মাধ্যমে কম্পাইল-টাইম অপ্টিমাইজেশনের কয়েকটি বিশেষ কৌশল ব্যবহার করা যায়, যা বড় প্রজেক্টে পারফরম্যান্স এবং মেইনটেন্যান্স উন্নত করে।
জেনেরিক্স ব্যবহার করলে কম্পাইলার টাইপের ত্রুটি (যেমন ClassCastException
) আগেই শনাক্ত করে।
public class GenericExample<T> {
private T value;
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public static void main(String[] args) {
GenericExample<String> example = new GenericExample<>();
example.setValue("Hello");
System.out.println(example.getValue());
// Compile-time error: incompatible types
// example.setValue(10);
}
}
জেনেরিক্সের মাধ্যমে টাইপ ইরেজার কম্পাইল-টাইমে সমস্ত জেনেরিক টাইপ মুছে দেয় এবং সেগুলোকে নির্দিষ্ট টাইপে কাস্ট করে।
public class GenericErasureExample<T> {
public void printClass(T obj) {
System.out.println(obj.getClass().getName());
}
public static void main(String[] args) {
GenericErasureExample<String> example = new GenericErasureExample<>();
example.printClass("Generics"); // Output: java.lang.String
}
}
Object
টাইপ ব্যবহার করে।public class GenericUtility {
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3};
String[] strArray = {"Java", "Generics"};
printArray(intArray); // Output: 1 2 3
printArray(strArray); // Output: Java Generics
}
}
import java.util.List;
public class WildcardExample {
public static void printList(List<?> list) {
for (Object obj : list) {
System.out.println(obj);
}
}
public static void main(String[] args) {
List<Integer> intList = List.of(1, 2, 3);
List<String> strList = List.of("A", "B", "C");
printList(intList); // Prints: 1 2 3
printList(strList); // Prints: A B C
}
}
public class BoundedTypeExample<T extends Number> {
private T value;
public BoundedTypeExample(T value) {
this.value = value;
}
public double getDoubleValue() {
return value.doubleValue();
}
public static void main(String[] args) {
BoundedTypeExample<Integer> intExample = new BoundedTypeExample<>(42);
System.out.println(intExample.getDoubleValue()); // Output: 42.0
BoundedTypeExample<Double> doubleExample = new BoundedTypeExample<>(3.14);
System.out.println(doubleExample.getDoubleValue()); // Output: 3.14
// Compile-time error: incompatible type
// BoundedTypeExample<String> strExample = new BoundedTypeExample<>("Test");
}
}
import java.util.HashMap;
import java.util.Map;
public class GenericContainer<K, V> {
private Map<K, V> map = new HashMap<>();
public void add(K key, V value) {
map.put(key, value);
}
public V get(K key) {
return map.get(key);
}
public static void main(String[] args) {
GenericContainer<String, Integer> container = new GenericContainer<>();
container.add("Age", 25);
System.out.println(container.get("Age")); // Output: 25
}
}
import java.lang.reflect.ParameterizedType;
public class GenericTypeToken<T> {
private Class<T> type;
@SuppressWarnings("unchecked")
public GenericTypeToken() {
this.type = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public Class<T> getType() {
return type;
}
public static void main(String[] args) {
GenericTypeToken<String> typeToken = new GenericTypeToken<String>() {};
System.out.println(typeToken.getType().getName()); // Output: java.lang.String
}
}
Read more